﻿#include "Card.h"


void InitializeCard(card &c, int value, char kind, int sequence)
{
	for (int i = 0; i < LEN_SYMBOL; i++)
	{
		c.symbol[i] = symbols[value - 2][i];
	}

	c.kind = kinds[kind - 1];
	c.sequence = sequence;
	c.value = value;

	if (kind == SPADE)
	{
		c.value += MAX_SPADE;
	}

	return;
}

void DisplayCard(const card &c, int x, int y, bool side)
{
	char currentCardVisual[CARD_VISUAL_HEIGHT][CARD_VISUAL_WIDTH];

	// Face à l'endroit.
	if (side == FACE)
	{
		memcpy(currentCardVisual, cardVisual, sizeof(char)*CARD_VISUAL_HEIGHT * CARD_VISUAL_WIDTH);

		// Remplacer les symboles à l'intérieur de la carte.
		currentCardVisual[symbolTopLeft.x][symbolTopLeft.y] = c.symbol[0];

		if (c.symbol[0] == '1')
		{
			currentCardVisual[symbolTopLeft.x][symbolTopLeft.y + 1] = c.symbol[1];
		}

		currentCardVisual[kindTopLeft.x][kindTopLeft.y] = c.kind;
		currentCardVisual[kindMiddle.x][kindMiddle.y] = c.kind;
		currentCardVisual[kindBottomRight.x][kindBottomRight.y] = c.kind;

		if (c.symbol[0] == '1')
		{
			currentCardVisual[symbolBottomRight.x][symbolBottomRight.y - 1] = c.symbol[0];
			currentCardVisual[symbolBottomRight.x][symbolBottomRight.y] = c.symbol[1];
		}
		else
		{
			currentCardVisual[symbolBottomRight.x][symbolBottomRight.y] = c.symbol[0];
		}
	}
	// Face à l'envers.
	else
	{
		memcpy(currentCardVisual, cardVisualBack, sizeof(char)*CARD_VISUAL_HEIGHT * CARD_VISUAL_WIDTH);

		// Remplacer le vide par un motif.
		for (int i = 1; i < CARD_VISUAL_HEIGHT - 1; i++)
		{
			for (int j = 0; j < CARD_VISUAL_WIDTH - 2; j++)
			{
				currentCardVisual[i][j] = cardBackFill;
			}
		}
	}

	// Remplacer le contour de la carte.
	currentCardVisual[0][0] = borders[CORNER_TOP_LEFT];
	currentCardVisual[0][CARD_VISUAL_WIDTH - 2] = borders[CORNER_TOP_RIGHT];
	currentCardVisual[CARD_VISUAL_HEIGHT - 1][0] = borders[CORNER_BOTTOM_LEFT];
	currentCardVisual[CARD_VISUAL_HEIGHT - 1][CARD_VISUAL_WIDTH - 2] = borders[CORNER_BOTTOM_RIGHT];

	for (int i = 1; i < CARD_VISUAL_HEIGHT - 1; i++)
	{
		currentCardVisual[i][0] = borders[CARD_SIDE];
		currentCardVisual[i][CARD_VISUAL_WIDTH - 2] = borders[CARD_SIDE];
	}

	for (int j = 1; j < CARD_VISUAL_WIDTH - 2; j++)
	{
		currentCardVisual[0][j] = borders[CARD_EDGE];
		currentCardVisual[CARD_VISUAL_HEIGHT - 1][j] = borders[CARD_EDGE];
	}


	// Afficher la carte aux bonnes coordonnées.
	for (int i = 0; i < CARD_VISUAL_HEIGHT; i++)
	{
		for (int j = 0; j < CARD_VISUAL_WIDTH; j++)
		{
			GotoXY(x + j, y + i);
			std::cout << currentCardVisual[i][j];
		}

		std::cout << std::endl;
	}

	return;
}


void GotoXY(int x, int y)
{
	COORD coord;
	coord.X = x;
	coord.Y = y;
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);

	return;
}


